﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace DentalSoftware
{
    public partial class BenhNhanForm : Form
    {
        private Form parent;
        public BenhNhanDTO kqtimBN;

        public BenhNhanForm(int width, int height)
        {
            InitializeComponent();
            this.Height = height;
            this.Width = width;
        }        

        private void BenhNhanForm_Load(object sender, EventArgs e)
        {
            parent = this.MdiParent;
            ShowForm();           
        }

        private void ShowForm()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = BenhNhanBUS.SelectBenhNhanByAll();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemBenhNhan frm = new ThemBenhNhan();
            if (frm.ShowDialog() == DialogResult.Yes)
            {
                ShowForm();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string textthongbao = String.Format("Bạn có chắc chắn muốn xóa bệnh nhân {0} không?", dataGridView1.CurrentRow.Cells[1].Value);
            if (MessageBox.Show(textthongbao, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                BenhNhanDTO bn = new BenhNhanDTO();
                bn.Ma = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                BenhNhanBUS.DeleteBenhNhan(bn);               
                MessageBox.Show("Đã xóa");
                BenhNhanForm_Load(sender, e);
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {            
            TimBenhNhan frm = new TimBenhNhan(this,1);
            if (frm.ShowDialog()== DialogResult.Yes)
            {
                dataGridView1.DataSource = BenhNhanBUS.SelectBenhNhanByThongTin(kqtimBN);
            }
        }

        private void btnXemDieuTri_Click(object sender, EventArgs e)
        {
            BenhNhanDTO bn = BenhNhanBUS.SelectBenhNhanByMa(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
            this.Close();            
            new DieuTriForm(this.Width, this.Height, bn) { MdiParent = parent }.Show();
            parent.Text = "Phòng răng _ Điều trị";
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            BenhNhanDTO bn = new BenhNhanDTO();
            bn.Ma = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            bn.Hoten = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            bn.Diachi = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            bn.Dienthoai = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            bn.DuaTruoc = float.Parse(dataGridView1.CurrentRow.Cells[8].Value.ToString());
            bn.Gioitinh = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
            bn.Manghenghiep = int.Parse(dataGridView1.CurrentRow.Cells[10].Value.ToString());
            bn.Maquan = int.Parse(dataGridView1.CurrentRow.Cells[11].Value.ToString());
            bn.Ngaysinh = DateTime.Parse(dataGridView1.CurrentRow.Cells[3].Value.ToString());
            bn.DaChupPhim = int.Parse(dataGridView1.CurrentRow.Cells[9].Value.ToString());
            ThemBenhNhan frm = new ThemBenhNhan(bn);            
            if (frm.ShowDialog() == DialogResult.Yes)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = BenhNhanBUS.SelectBenhNhanByHoTen(txtTimBenhNhan.Text);
            }
        }

        private void txtTimBenhNhan_TextChanged(object sender, EventArgs e)
        {
            if (txtTimBenhNhan.Text != "")
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = BenhNhanBUS.SelectBenhNhanByHoTen(txtTimBenhNhan.Text);
            }
            else
            {
                dataGridView1.DataSource = BenhNhanBUS.SelectBenhNhanByAll();
            }
        }        
    }
}
